package spring.aop;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 *
 * advice use case
 */
@Component("AdviceTest")
@Aspect
public class AdviceTest {
	@Around("execution(* *Students(..))")
	public Object process(ProceedingJoinPoint point) throws Throwable {
		System.out.println("@Around:执行目标方法之前。。");
		Object[] args = point.getArgs();
		if (args != null && args.length > 0 && args[0].getClass() == String.class) {
			args[0] = "改变后的参数1";
		}
		Object returnValue = point.proceed(args);
		System.out.println("@Around:执行方法之后。。。");
		System.out.println("@Around:被植入的目标对象为:" + point.getTarget());
		return "原返回值：" + returnValue + ",这是返回结果的后缀";
	}

	@Before("execution(* *Students(..)))")
	public void permissionCheck(JoinPoint point) {
		System.out.println("Before:模拟权限检查");
		System.out.println(
				"@Before目标方法为:" + point.getSignature().getDeclaringTypeName() + "." + point.getSignature().getName());
		System.out.println("@Before参数为：" + Arrays.toString(point.getArgs()));
		System.out.println("@Before:被植入的目标对象为：" + point.getTarget());
	}

	@AfterReturning(pointcut="execution(* *Students(..))",returning="returnValue")
   public void log(JoinPoint point,Object returnValue){
	   System.out.println("AfterReturning:模拟记录日志功能....");
	   System.out.println("AfterReturning:目标方法为："+point.getSignature().getDeclaringTypeName()+"."+point.getSignature().getName());
	   System.out.println("@AfterRuturning:参数为："+Arrays.toString(point.getArgs()));
	   System.out.println("@AfterRuturning:返回值为"+returnValue);
	   System.out.println("@AfterRuturning:被织入的目标对象为："+point.getTarget());

   }
	@After("execution(* *Students(..))")
	public void releaseResource(JoinPoint point){
		System.out.println("After 模拟释放资源。。。。。");
	}
}
